package com.didispace;

import java.util.concurrent.TimeUnit;

import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;

/**
 * Created by Administrator on 2016/9/16.
 */
public class UserCommand extends HystrixCommand<User> {

    private RestTemplate restTemplate;
    
    private String id;
    
    public UserCommand(RestTemplate restTemplate, String id) {
    	super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
    	this.restTemplate=restTemplate;
    	this.id=id;
    }
    

	@Override
	protected User run() throws Exception {
		//  超市默认一秒
		//TimeUnit.SECONDS.sleep(3); 
		return restTemplate.getForObject("http://eureka-client/users/{1}", User.class, id);
	}
	
	// 同步执行 User u = new UserCommand(restTemplate, 1L).execute();
	// 异步执行 Future<User> futureUser = new UserCommand(restTemplate,1L).queue();
	@Override
	protected User getFallback() {
		System.out.print("getFallback");
		return null;
	}
	
}
